


import { Button, message, Space, Table } from "antd"
import moment from "moment"
import { useRef, useState } from "react"
import { useRequest } from "umi"
import ChooseTable from "./chooseTable"

const ChooseManger = () => {
  const modelComRef: any = useRef(null);
  const { data, loading } = useRequest({
    url: '/api/course/findAll'
  })
  const { run: chooseApi, loading: chooseLoading } = useRequest((req) => ({
    url: '/api/choose-course',
    method: 'post',
    data: req
  }), { manual: true })

  const [tableId, setTableId] = useState<string | null>(null)

  const handleChoose = async (id: string) => {
    setTableId(id)
    await chooseApi({ courseId: id })
    setTableId(null)
    message.success('操作成功')
    modelComRef.current.init()
  }
  const columns = [
    { title: '课程名', dataIndex: 'courseName' },
    { title: '授课教师', dataIndex: 'teacherName' },
    { title: '开始节次', dataIndex: 'startSection' },
    { title: '结束节次', dataIndex: 'endSection' },
    { title: '星期', dataIndex: 'weeks' },
    { title: '创建时间', dataIndex: 'createTime', render: (text: string) => (
      moment(text).format('yyyy-MM-DD HH:mm:ss')
    ) },
    { title: '操作', render: (text: any, record: any) => (
      <Space>
        <Button onClick={() => handleChoose(record.id) } loading={ tableId == record.id && chooseLoading} size="small" type="primary">选择</Button>
      </Space>
    ) },
  ]

  return <>
    <Table rowKey='id' dataSource={data} loading={loading} columns={columns} />
    <ChooseTable cRef={modelComRef}/>
  </>
}

export default ChooseManger